//
//  XJOAuthController.m
//  weiboOC
//
//  Created by 徐绩 on 16/4/21.
//  Copyright © 2016年 ayumi. All rights reserved.
//

#import "XJOAuthController.h"
#import "XJUserAccountViewModel.h"

@interface XJOAuthController ()<UIWebViewDelegate>

@property (nonatomic,strong) UIWebView *webView;

@end

static NSString *APP_KEY = @"3663247462";
static NSString *REDIRECT_URI = @"http://www.baidu.com/";
static NSString *App_SECRECT = @"b87e34ba9aefdfcca7e4d0fbaf45862e";

@implementation XJOAuthController

-(UIWebView *)webView{

    if (_webView == nil) {
        
        _webView = [[UIWebView alloc]init];
        _webView.delegate = self;
    }
    return _webView;
}

-(void)loadView{
    
    [super loadView];
    self.view = self.webView;
    
     //请求授权
    NSString *url = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",APP_KEY,REDIRECT_URI];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    [self.webView loadRequest:request];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

-(void)setupUI{

    self.title = @"新浪微博";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(didClose)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"自动填充" style:UIBarButtonItemStylePlain target:self action:@selector(autoFill)];
}

#pragma mark - 点击关闭
-(void)didClose{

    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    [SVProgressHUD dismiss];
}

#pragma mark - 自动填充
-(void)autoFill{
    
    NSString *str = @"document.getElementById('userId').value='1009266532@qq.com';document.getElementById('passwd').value='75583282xuji'";
    
    [self.webView stringByEvaluatingJavaScriptFromString:str];
}

#pragma mark - webView代理方法
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
//    code从请求地址中获取
    NSURL *url = request.URL;
    NSString *query = url.query;

    
    if (url == nil) {
        return NO;
        
    }else if([url.absoluteString hasPrefix:REDIRECT_URI]){
        
        if (query && [query containsString:@"code="]) {
            
            //获取授权码
            NSString *code = [query substringFromIndex:5];
            
            [[XJUserAccountViewModel sharedViewModel]loadAccessToken:code isSuccess:^(BOOL isSuccess) {
                if (isSuccess) {
                    
                    [self dismissViewControllerAnimated:NO completion:^{
                        
                        //  切换控制器到欢迎页
                        [[NSNotificationCenter defaultCenter]postNotificationName:@"changeRootVCNotification" object:nil];
                    }];
                    
                }else{
                    
                    Log(@"登录失败");
                }
            }];
        }
        return NO;
    }
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView{
    [SVProgressHUD show];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [SVProgressHUD dismiss];
}



@end
